//
//  ReferralViewController.m
//  WuJieLive
//
//  Created by iMac on 2017/10/19.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "ReferralViewController.h"
#import "UserProtocolViewController.h"

@interface ReferralViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *userImage;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *userPhoneNum;

@end

@implementation ReferralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)setNavi
{
    self.title = @"注册";
}

-(void)setUI
{
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:self.headerImageString]];
    self.userName.text = self.nickNameString;
    self.userPhoneNum.text = self.telePhoneString;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 提交
- (IBAction)commit:(id)sender {
    
    if (self.popType == 0) { // 0是从注册过来的
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"推荐人一经绑定不能修改，确定是此联系人吗?" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction * done = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [MBProgressHUD showMessage:@"正在注册" toView:self.view];  // 没有推荐人
            NSDictionary * dict = @{@"phone_redis_key":self.redisKey,@"password":self.password};
            [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/register",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
                if (isSuccess) {
                    NSDictionary * dic = @{@"phone_mob":self.telephoneNumber,@"password":self.password};
                    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/login",BaseUrl] para:dic success:^(BOOL isSuccess, id responseObject) {
                        [MBProgressHUD hideHUDForView:self.view];
                        if (isSuccess) {
                            self.isLogin = YES;
                            self.userToken = responseObject[@"data"][@"token"];
                            [self dismissViewControllerAnimated:YES completion:nil];
                        }
                    } failure:^(NSError *error) {
                        [MBProgressHUD hideHUDForView:self.view];
                    }];
                }
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view];
            }];
        }];
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:done];
        [alert addAction:cancle];
        [self presentViewController:alert animated:YES completion:nil];
    
    }else
    {// 非0是从编辑
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"推荐人一经绑定不能修改，确定是此联系人吗?" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction * done = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [MBProgressHUD showMessage:@"正在绑定推荐人" toView:self.view];
            NSDictionary * dict = @{@"token":self.userToken,@"promoter_redis_key":self.referenceKey};
            [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/save-reference",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (isSuccess) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
        }];
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:done];
        [alert addAction:cancle];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
